package com.glory.brepda.activity.mfg;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.LotLists;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class AllInOneMultiEquipmentAbortActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLot)
    EditText etLot;
    @BindView(R.id.tvLotQTY)
    TextView tvLotQTY;
    @BindView(R.id.tvLotState)
    TextView tvLotState;
    @BindView(R.id.tvStepDesc)
    TextView tvStepDesc;
    @BindView(R.id.etComment)
    EditText etComment;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.lotScan)
    ImageView lotScan;
    private static final int REQUEST_CODE_TEXT1 = 1001;
    private String lotRRN = "";
    private String EqpId;
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_work_abort;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        initEdit(etLot, s -> LotUtils.searchLotInfo(AllInOneMultiEquipmentAbortActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
            @Override
            public void onSuccess(LotInfoEntity response) {
                tvTitle.setText(response.getLOT().getSTEPDESC());
                tvLotQTY.setText(response.getLOT().getMAINQTY());
                tvLotState.setText(response.getLOT().getSTATE());
                tvStepDesc.setText(response.getLOT().getSTEPDESC());
                lotRRN = response.getLOT().getORGRRN();
                EqpId = response.getLOT().getEQUIPMENTID();
            }

            @Override
            public void onFail(Throwable t) {

            }
        }));
    }

    @Override
    protected void initData() {

        lotScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startQrScanner(REQUEST_CODE_TEXT1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null) {
            String qrResult = data.getStringExtra("SCAN_RESULT"); // 获取扫码结果
            if (qrResult != null) {
                switch (requestCode) {
                    case REQUEST_CODE_TEXT1:
                        etLot.setText(qrResult);
                        break;
                }
            }

        }
    }


    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                        (new TypeToken<BaseResponseBean<JSONObject>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                            showConfirmDialog(AllInOneMultiEquipmentAbortActivity.this,"abort成功");
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<JSONObject>> response) {
                        super.onError(response);
                        loadError(response.getException(),"LOTTRACKIN");
                    }
                };

                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE","");
                map.addProperty("ACTIONCOMMENT",getText(etComment));
                map.addProperty("EQUIPMENTID",EqpId);
                List<LotLists> lotList = new ArrayList<>();
                lotList.add(new LotLists(getText(etLot)));
                map.add("LOTLIST", new JsonParser().parse(new Gson().toJson(lotList)).getAsJsonArray());//传批次集合

                NetUtils.requestPostNet("IF.MULTIMASTERLOTABORT","MultiMasterLotAbort",AllInOneMultiEquipmentAbortActivity.this,map,callBack);
                break;
        }
    }
}
